// d:\miniprograms\shenye-dy-applet\components\city-picker\city-picker.js
import {
  getCommonCity
} from '../../api/my.js'
Component({
  data: {
    value: [],
    addressList: [],
    provinceList: [],
    cityList: [],
    areaList: [],
    indicatorStyle: `height: 50px;`,
    provinceIndex: 0,
    cityIndex: 0,
    areaIndex: 0,
    animationData: {}
  },
  properties: {
    defaultValue: {
      type: String,
      value: ''
    },
    visible: {
      type: Boolean,
      value: false
    },
    maskCloseAble: {
      type: Boolean,
      value: true
    }
  },
  created() {
    this.getCommonCityFunc()
  },
  methods: {
    // 获取数据
    getCommonCityFunc() {
      getCommonCity().then(res => {
        this.data.addressList = res.result
        var provinceList = []
        this.data.addressList.filter(item => {
          provinceList.push({
            id: item.id,
            label: item.label
          })
        })
        this.data.provinceList = [...provinceList]
        if (!this.data.defaultValue) {
          this.data.cityList = this.data.addressList[0].children
          this.data.areaList = this.data.addressList[0].children[0].children
        } else {
          var city = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江 ", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北 ", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏 ", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外 " }
          if (city[this.data.defaultValue.substr(0, 2)]) {
            var province = this.data.defaultValue.substr(0, 2)
            var city = this.data.defaultValue.substr(0, 4)
            this.data.provinceIndex = 0
            this.data.cityIndex = 0
            this.data.areaIndex = 0
            this.data.provinceIndex = this.data.addressList.findIndex(obj => {
              return obj.id == province
            })
            this.data.provinceIndex = this.data.provinceIndex >= 0 ? this.data.provinceIndex : 0
            this.data.cityList = this.data.addressList[this.data.provinceIndex].children
            this.data.cityIndex = this.data.cityList.findIndex(obj => {
              return obj.id == city
            })
            this.data.cityIndex = this.data.cityIndex >= 0 ? this.data.cityIndex : 0
            this.data.areaList = this.data.cityList[this.data.cityIndex].children
            console.log(this.data.areaList, this.data.defaultValue)
            this.data.areaIndex = this.data.areaList.findIndex(obj => {
              return obj.id == this.data.defaultValue
            })
            this.data.areaIndex = this.data.areaIndex >= 0 ? this.data.areaIndex : 0
            console.log(this.data.areaIndex)
            this.data.value = [...[this.data.provinceIndex, this.data.cityIndex, this.data.areaIndex]]
          } else {
            uni.showToast({
              title: '地区编码格式不正确',
              icon: 'none'
            })
            console.log('地区编码格式不正确')
          }
        }
        this.setData({
          cityList: this.data.cityList,
          provinceList: this.data.provinceList,
          areaList: this.data.areaList,
          areaIndex: this.data.areaIndex,
          value: this.data.value,
          cityIndex: this.data.cityIndex,
          provinceIndex: this.data.provinceIndex,

        })
        // this.changeActive()
      })
    },
    bindChange(e) {
      const val = e.detail.value
      val[0] = val[0] || 0
      val[1] = val[1] || 0
      val[2] = val[2] || 0
      if (val[0] != this.data.provinceIndex) {
        // console.log('监听第一列')
        this.provinceChange(val[0])
      } else if (val[1] != this.data.cityIndex) {
        // console.log('监听第二列')
        this.cityChange(val[1])
      } else if (val[2] != this.data.areaIndex) {
        // console.log('监听第三列')
        this.areaChange(val[2])
      }
    },
    // 监听第一列变化
    provinceChange(e) {
      this.setData({
        cityList: this.data.addressList[e].children,
        areaList: this.data.addressList[e].children[0].children,
        areaIndex: 0,
        value: [...[e, 0, 0]],
        cityIndex: 0,
        provinceIndex: e,
      })
    },
    // 监听第二列变化
    cityChange(e) {
      this.setData({
        cityList: this.data.addressList[this.data.provinceIndex].children,
        areaList: this.data.addressList[this.data.provinceIndex].children[e].children,
        areaIndex: 0,
        value: [...[this.data.provinceIndex, e, 0]],
        cityIndex: e,
      })
    },
    // 监听第三列变化
    areaChange(e) {
      this.setData({
        areaIndex: e,
      })
    },
    // 打开弹窗
    openPopup() {
      this.open('cityPicker')
    },
    // 关闭弹窗
    closePopup() {
      this.close('cityPicker')
    },
    // 取消
    cancel() {
      this.close('cityPicker')
      this.triggerEvent('cancel')
    },
    // // 确认
    confirm() {
      this.close('cityPicker')
      this.triggerEvent('confirm', {
        id: this.data.addressList[this.data.provinceIndex].children[this.data.cityIndex].children[this.data.areaIndex].id,
        label: this.data.addressList[this.data.provinceIndex].label + ' ' + this.data.addressList[this.data.provinceIndex].children[this.data.cityIndex].label + ' ' + this.data.addressList[this.data.provinceIndex].children[this.data.cityIndex].children[this.data.areaIndex].label
      })
    },
    // 打开弹窗
    open(popupName) {
      const childComponent = this.selectComponent('#' + popupName);
      childComponent.open();
    },
    // 关闭弹窗
    close(popupName) {
      const childComponent = this.selectComponent('#' + popupName);
      childComponent.close();
    },
  }
})